package com.netty;

import com.common.Global;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.corundumstudio.socketio.AckRequest;
import com.corundumstudio.socketio.SocketIOClient;
import com.corundumstudio.socketio.SocketIOServer;
import com.corundumstudio.socketio.annotation.OnConnect;
import com.corundumstudio.socketio.annotation.OnDisconnect;
import com.corundumstudio.socketio.annotation.OnEvent;

@Component
public class MessageEventHandler {

	private final SocketIOServer server;

	@Autowired
	public MessageEventHandler(SocketIOServer server) {
		this.server = server;
	}

	@OnConnect
	public void onConnect(SocketIOClient client) {
//		String clientId = client.getHandshakeData().getSingleUrlParam("clientid");
	}

	@OnDisconnect
	public void onDisconnect(SocketIOClient client) {
//		String clientId = client.getHandshakeData().getSingleUrlParam("clientid");
	}

	//消息接收入口，当接收到消息后，查找发送目标客户端，并且向该客户端发送消息，且给自己发送消息
	@OnEvent(value = "messageevent")
	public void onEvent(SocketIOClient client, AckRequest request, MessageInfo data) {
		String targetClientId = data.getTargetClientId();
			MessageInfo sendData = new MessageInfo();
			sendData.setSourceClientId(data.getSourceClientId());
			sendData.setTargetClientId(data.getTargetClientId());
			sendData.setMsgType("chat");
			sendData.setMsgContent(data.getMsgContent());
			client.sendEvent("messageevent", sendData);
			server.getClient(Global.CLIENTUUID).sendEvent("messageevent", sendData);

	}
}

